<?php
namespace Home\Model;
use Common\Model\BaseModel;

class FaultRecordsModel extends BaseModel {

    public $tableName = 'fault_records';

    /**
     * @param $post
     * @return array
     * 更新或修改合同信息
     */
    public function saveFaultRecords($data){

        $ret = array('status'=>true,'msg'=>'');
        $post = $this->returnData($data);

        if(!$this->create($post)){

            $ret['status'] = false;
            $ret['msg'] = $this->getError();

        }else{

            
            $res = (null !== $post['fault_records_id']) ? $this->save($post) : $this->add($post);
            
            if(false !== $res){
                $ret['msg'] = '操作成功';
            }else{
                $ret['status'] = false;
                $ret['msg'] = $this->getDbError();
            }
        }

         return $ret;
    }

    public function returnData($post){

        $return = array();

        $return['fault_records_id']    = !empty($post['fault_records_id']) ? intval($post['fault_records_id']) : null;
        $return['equipment_id']        = !empty($post['equipment_id']) ? intval($post['equipment_id']) : '';
        $return['records_no']          = !empty($post['records_no']) ? addslashes($post['records_no']) : '';
        $return['fault_time']          = !empty($post['fault_time']) ? strtotime($post['fault_time']) : time();
        $return['fault_reason']        = !empty($post['fault_reason']) ? addslashes($post['fault_reason']) : '';
        $return['deal_method']         = !empty($post['deal_method']) ? addslashes($post['deal_method']) : '';
        $return['status']              = !empty($post['status']) ? addslashes($post['status']) : '';
        $return['reg_time']            = !empty($post['reg_time']) ? strtotime($post['reg_time']) : time();
        $return['del']                 = 1;
        $return['years_month']                = !empty($post['fault_time']) ? date('Ym',strtotime($post['fault_time'])):date('Ym',time());
        $return['m']                = !empty($post['fault_time']) ? date('m',strtotime($post['fault_time'])):date('m',time());
        return $return;
    }
    #总体数据统计
    public function getTotalFault($stime,$etime){

        $fields   = "equipment_id,COUNT(fault_records_id) as totals ";
        $where    = " del=1 and fault_time>={$stime} and fault_time<=$etime";
        $groupby  = " equipment_id ";
        $orderby  = ' totals DESC ';
        $sql = "SELECT {$fields} FROM fault_records WHERE {$where} GROUP BY {$groupby} ORDER BY {$orderby}";
        
        $res = $this->query($sql);
        return !empty($res) ? $res : array();
    }

    #分设备信息统计
    public function getStatistiscByEquId($equipment_id){
        
        define('MONTH_NUM', 12);
        $stime = strtotime(date('Y').'-01-01');
        $etime = time();

        #十二个月初始化数据为0
        $return = array();
        for ($i=0; $i < MONTH_NUM; $i++) { 
            $return[$i] = 0;
        }

        $fields   = " COUNT(fault_records_id) as totals,years_month";
        $where    = " equipment_id={$equipment_id} and del=1 and fault_time BETWEEN {$stime} and {$etime} ";
        $groupby  = " years_month ";
        // $orderby  = ' ';ORDER BY {$orderby}
        $sql = "SELECT {$fields} FROM fault_records WHERE {$where} GROUP BY {$groupby} ";
        
        $res = $this->query($sql);

        if(!empty($res)){
            for ($i=0; $i < MONTH_NUM; $i++) { 
                foreach ($res as $key => $value) {
                    if(($i+1) == substr($value['years_month'], -2,2)){
                         $return[$i] = intval($value['totals']);
                         break;
                    }
                }
            }
        }
        return $return;
    }
}